package com.syh.jn.scm.domain.downstream.entity.base.logistics;

import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.downstream.repository.finance.DownStatementAccountBillReadRepository;
import com.syh.jn.scm.domain.downstream.repository.purchase.DownPurchaseOrderReadRepository;
import com.syh.jn.scm.domain.downstream.repository.purchase.DownPurchaseReturnOrderReadRepository;
import com.syh.jn.scm.dto.downstream.base.logistics.DownLogisticsMto;
import com.syh.jn.scm.dto.downstream.base.logistics.DownLogisticsSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 物流
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class DownLogistics extends AbstractSimpleAggregate<Long> {
  public DownLogistics() {
  }

  /**
   * 构造方法增加物流
   *
   * @param sto
   */
  public DownLogistics(DownLogisticsSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);

    this.createTimeStamp = System.currentTimeMillis();
    this.status = Short.valueOf("1");
    ExtBeanUtils.copyProperties(sto, this);
  }

  /**
   * 修改经营主体
   *
   * @param mto
   */
  public void modify(DownLogisticsMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);
    ExtBeanUtils.copyProperties(mto, this);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 数据销毁
   */
  public void destroy() {
    //查看采购是否有数据存在
    DownPurchaseOrderReadRepository purchase = (DownPurchaseOrderReadRepository) SpringContextUtils.getBean(DownPurchaseOrderReadRepository.class);
    Wrapper purchaseWrapper = Wrapper.build();
    purchaseWrapper.append(Restrictions.eq("logisticsCompanyId", this.id));
    Integer purchaseCount = purchase.getCount(purchaseWrapper);
    if (purchaseCount > 0) {
      throw new BusinessException("采购单存在该数据,删除失败");
    }
    //查看采购退货是否有数据存在
    DownPurchaseReturnOrderReadRepository purchaseReturn = (DownPurchaseReturnOrderReadRepository) SpringContextUtils.getBean(DownPurchaseReturnOrderReadRepository.class);
    Wrapper purchaseReturnWrapper = Wrapper.build();
    purchaseReturnWrapper.append(Restrictions.eq("logisticsCompanyId", this.id));
    Integer purchaseReturnCount = purchaseReturn.getCount(purchaseReturnWrapper);
    if (purchaseReturnCount > 0) {
      throw new BusinessException("采购退货单存在该数据,删除失败");
    }
    //查看对账单是否有数据存在
    DownStatementAccountBillReadRepository statementBill = (DownStatementAccountBillReadRepository) SpringContextUtils.getBean(DownStatementAccountBillReadRepository.class);
    Wrapper statementBillWrapper = Wrapper.build();
    //下游与物流对账情况未知，先按服务商的逻辑写
    statementBillWrapper.append(Restrictions.eq("customerId", this.id));
    statementBillWrapper.append(Restrictions.eq("customerType", Short.valueOf("2")));
    Integer statementCount = statementBill.getCount(statementBillWrapper);
    if (statementCount > 0) {
      throw new BusinessException("对账单存在该数据,删除失败");
    }
  }

  /**
   * 启用
   */
  public void enabled() {
    this.status = Short.valueOf("1");
  }

  /**
   * 停用
   */
  public void disabled() {
    this.status = Short.valueOf("-1");
  }

  /**
   * 物流公司名字
   */
  private String name;
  /**
   * 公司的联系电话
   */
  private String telephoneNum;
  /**
   * 是否提供冷链车: -1不提供,1提供
   */
  private Short isProvide;
  /**
   * 状态: -1停用, 1可用
   */
  private Short status;
  /**
   * 归属id
   */
  private Long businessEntityId;
  /**
   * 期初应收总额
   */
  private BigDecimal beginReceiveTotalAmount;
  /**
   * 期初应付总额
   */
  private BigDecimal beginPaymentTotalAmount;
}
